#include "load_ops.h"

// "Load" operation hierarchy
//  Wrappers for Mix_LoadChunk, Mix_LoadMUS, and IMG_Load
//  which attempt multiple extensions

extern int mix_flags;
extern int image_flags;

Mix_Music *load_music(const char *filename)
{
  Mix_Music *mus;
  char fullname[256];

  if (mix_flags & MIX_INIT_FLAC)
  {
    sprintf(fullname, "data/audio/%s.flac", filename);
    if ((mus = Mix_LoadMUS(fullname))) return mus;
  }

  if (mix_flags & MIX_INIT_OGG)
  {
    sprintf(fullname, "data/audio/%s.ogg", filename);
    if ((mus = Mix_LoadMUS(fullname))) return mus;
  }

  if (mix_flags & MIX_INIT_MP3)
  {
    sprintf(fullname, "data/audio/%s.mp3", filename);
    if ((mus = Mix_LoadMUS(fullname))) return mus;
  }

  sprintf(fullname, "data/audio/%s.wav", filename);
  return Mix_LoadMUS(fullname);
}

Mix_Chunk *load_wave(const char *filename)
{
  Mix_Chunk *wav;
  char fullname[256];

  if (mix_flags & MIX_INIT_FLAC)
  {
    sprintf(fullname, "data/audio/%s.flac", filename);
    if ((wav = Mix_LoadWAV(fullname))) return wav;
  }

  if (mix_flags & MIX_INIT_OGG)
  {
    sprintf(fullname, "data/audio/%s.ogg", filename);
    if ((wav = Mix_LoadWAV(fullname))) return wav;
  }

  if (mix_flags & MIX_INIT_MP3)
  {
    sprintf(fullname, "data/audio/%s.mp3", filename);
    if ((wav = Mix_LoadWAV(fullname))) return wav;
  }

  sprintf(fullname, "data/audio/%s.wav", filename);
  return Mix_LoadWAV(fullname);
}

SDL_Surface *load_image(const char *filename)
{
  SDL_Surface *img;
  char fullname[256];

  if (image_flags & IMG_INIT_PNG)
  {
    sprintf(fullname, "data/image/%s.png", filename);
    if ((img = IMG_Load(fullname))) return img;
  }

  if (image_flags & IMG_INIT_TIF)
  {
    sprintf(fullname, "data/image/%s.tif", filename);
    if ((img = IMG_Load(fullname))) return img;
  }

  if (image_flags & IMG_INIT_JPG)
  {
    sprintf(fullname, "data/image/%s.jpg", filename);
    if ((img = IMG_Load(fullname))) return img;
  }

  sprintf(fullname, "data/image/%s.bmp", filename);
  return IMG_Load(fullname);
}
